import 'package:flutter/material.dart';

class TagsWidget extends StatelessWidget {
  final String name;
  final Color color;
  final Color bgcolor;
  const TagsWidget.tag(this.name,
      {Key? key, this.color = Colors.black, this.bgcolor = Colors.amberAccent})
      : super(key: key);

// factory关键字，工厂模式，创建一个构造函数
  factory TagsWidget(
    String name,
  ) {
    switch (name) {
      case '近地铁':
        return TagsWidget.tag(
          name,
          color: Colors.redAccent,
          bgcolor: const Color.fromARGB(100, 238, 44, 44),
        );
      case '新上':
        return TagsWidget.tag(
          name,
          color: const Color.fromARGB(255, 210, 105, 30),
          bgcolor: const Color.fromARGB(100, 210, 105, 30),
        );
      default:
        return TagsWidget.tag(
          name,
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(0, 3, 3, 0),
      padding: const EdgeInsets.all(5),
      decoration: BoxDecoration(
          color: bgcolor, borderRadius: BorderRadius.circular(10)),
      child: Text(
        name,
        style: TextStyle(color: color),
      ),
    );
  }
}
